//
//  PLVLiveWarmPlayer.m
//  rzc_app
//
//  Created by 孙磊 on 2019/9/9.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import "PLVLiveWarmPlayer.h"
#import <React/RCTConvert.h>
#import <IJKMediaFramework/IJKMediaFramework.h>
#import <PLVVodSDK/PLVVodPlayerViewController.h>

@interface PLVLiveWarmPlayer()

@property (nonatomic, strong) NSString *vid;
@property (nonatomic, strong) PLVVodPlayerViewController *subPlayer;
@property (nonatomic, assign) CGRect originFrame;   // 初始设置的frame
@property  (nonatomic, assign)  CGRect fullScreenRect;

@end

@implementation PLVLiveWarmPlayer


- (instancetype)initWithVid:(NSString *)vid displayView:(UIView *)displayView{
  self = [super initWithFrame:displayView.bounds];
  CGRect screenBounds = [UIScreen mainScreen].bounds;
  CGFloat screenWidth = screenBounds.size.width;
  CGFloat screenHeight = screenBounds.size.height;
  CGFloat width = MIN(screenWidth, screenHeight);
  _originFrame = CGRectMake(0, 0, width, width/16*9);
  _vid = vid;
  [displayView addSubview:self];
  if (self) {
    [self playWithVideoCover];
  }
  return self;
}

- (void)dealloc {
  RCTLog(@"-[%@ %@]",NSStringFromClass([self class]),NSStringFromSelector(_cmd));
}

- (void)play {
  if (self.subPlayer) {
    if (self.subPlayer.playbackState != IJKMPMoviePlaybackStatePlaying) {
      RCTLog(@"self.subPlayer play");
      [self.subPlayer play];
    }
  }
}
- (void)pause{
  if (self.subPlayer) {
    if (self.subPlayer.playbackState != IJKMPMoviePlaybackStatePaused) {
      RCTLog(@"self.subPlayer pause");
      [self.subPlayer pause];
    }
  }
}

- (void)clearPlayer {
  if (_subPlayer) {
    [_subPlayer destroyPlayer];
    _subPlayer = nil;
  }
  [self removeFromSuperview];
}

- (void)setOrientationLandscape {
  [UIView animateWithDuration:0.3 animations:^{
    RCTLog(@"setOrientationLandscapeframe%@",NSStringFromCGRect([UIScreen mainScreen].bounds));
    self.frame = [UIScreen mainScreen].bounds;
    self.subPlayer.view.frame =  [UIScreen mainScreen].bounds;////
//    UIInterfaceOrientation interfaceOrientation = (UIInterfaceOrientation)[[UIDevice currentDevice] orientation];
//    [PLVVodPlayerViewController rotateOrientation:interfaceOrientation];
  }];
}

- (void)setOrientationPortrait {
  [UIView animateWithDuration:0.3 animations:^{
    RCTLog(@"setOrientationPortraitframe%@",NSStringFromCGRect(self->_originFrame));
    self.frame = self->_originFrame;
    self.subPlayer.view.frame = self->_originFrame;
//    UIInterfaceOrientation interfaceOrientation = (UIInterfaceOrientation)[[UIDevice currentDevice] orientation];
//    [PLVVodPlayerViewController rotateOrientation:interfaceOrientation];
  }];
}
#pragma mark - Private

- (void)playWithVideoCover {
  if (_vid) {
    self.subPlayer = [[PLVVodPlayerViewController alloc] initWithNibName:nil bundle:nil];
    self.subPlayer.rememberLastPosition = NO;
    self.subPlayer.enableBackgroundPlayback = YES;
    self.subPlayer.autoplay = NO;
    self.subPlayer.enablePlayRecycle = YES;
    [self addSubview:self.subPlayer.view];
    
    CGRect frame = self.subPlayer.view.frame;
    frame.size = self.frame.size;
    self.subPlayer.view.frame = frame;
    self.subPlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        
    CGRect screenBounds = [UIScreen mainScreen].bounds;
    CGFloat screenWidth = screenBounds.size.width;
    CGFloat screenHeight = screenBounds.size.height;
    self.fullScreenRect = CGRectMake(0, 0, screenHeight, screenWidth);
    __weak typeof(self) weakSelf = self;
    [PLVVodVideo requestVideoWithVid:_vid completion:^(PLVVodVideo *video, NSError *error) {
      if (!video.available) {
        return;
      }
      weakSelf.subPlayer.video = video;
    }];
    [self.subPlayer setPlayerErrorHandler:^(PLVVodPlayerViewController *player, NSError *error) {
      RCTLog(@"player setPlaybackStateHandler%@",error);
    }];
    [self.subPlayer setPlaybackStateHandler:^(PLVVodPlayerViewController *player) {
      RCTLog(@"player setPlaybackStateHandler%ld",(long)player.playbackState);
      if (player.playbackState == PLVVodPlaybackStatePlaying) {
        if (weakSelf.playCoverBegin) {
          weakSelf.playCoverBegin();
        }
      } else if (player.playbackState == PLVVodPlaybackStatePaused || player.playbackState == PLVVodPlaybackStateStopped || player.playbackState == PLVVodPlaybackStateInterrupted) {
        if (weakSelf.playCoverEnd) {
          weakSelf.playCoverEnd();
        }
      }
    }];
  }
}
@end
